<template>
	<view class="page" style="padding-bottom: 50rpx;">
		<view class="topVue">
			<view class="safeVue" :style="{ height: statusBarHeight + 'rpx' }"></view>
			<view class="flex align-center padding-row30" :style="{ height: barHeight + 'rpx', top: statusBarHeight + 'rpx' }" @click="$tools.back()">
				<image src="../../static/back.png" style="width: 48rpx; height: 48rpx;"></image>
				<text style="line-height: 1; color: #454545; font-weight: 600; font-size: 32rpx; padding-left: 20rpx;">邀请海报</text>
			</view>
		</view>
		<view :style="{ height: statusBarHeight + barHeight + 'rpx' }"></view>
		<view class="box margin0">
			<view class="canvas">
				<canvas style="width: 300px; height: 533px;" canvas-id="canvasId"></canvas>
			</view>
		</view>
		<view class="btn margin20"  @click="sharePoster">点击分享邀请海报</view>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	import QR from "@/common/qrcode/qrcode.js";
	export default {
		data(){
			return {
				statusBarHeight: 0,
				barHeight: 0,
				cansWidth: 300, //海报宽度
				cansHeight: 533,
				poster: "https://jg.zgxingongxiang.com/test/static/WeChat/5.jpg"
			}
		},
		onLoad() {
			let that = this;
			wx.getSystemInfo({
				success(res) {
					let deviceWidth = wx.getSystemInfoSync().windowWidth
					that.statusBarHeight = Number(res.statusBarHeight) * ( 750 / deviceWidth ) 
					let menuButtonInfo = wx.getMenuButtonBoundingClientRect()
					let barHeight = menuButtonInfo.height + (menuButtonInfo.top - res.statusBarHeight) * 2
					that.barHeight = Number(barHeight) * ( 750 / deviceWidth )
				},
			});
		},
		computed: {
			...mapState({
				user: state=> state.user
			})
		},
		onReady() {
			setTimeout(()=>{
				this.initCanvas()
			}, 500)
		},
		methods: {
			initCanvas(){
				this.$request("/api/WeChat/queryShareInfo", {}, "GET").then(result=>{
					console.log("邀请链接", result.data.redirectUrl)
					let img = QR.createQrCodeImg(result.data.redirectUrl, {
						typeNumber: 10,
						size: parseInt(140)
					})
					console.log(img)
					let that = this;
					const ctx = wx.createCanvasContext("canvasId");
					wx.getFileSystemManager().writeFile({
						filePath: `${wx.env.USER_DATA_PATH}/poster.png`, // 指定文件路径
						data: img.replace(/^data:image\/\w+;base64,/, ''), // 移除 base64 前缀
						encoding: 'base64', // 指定编码方式为 base64
						success(res){
							console.log(res)
							wx.getImageInfo({
								src: `${wx.env.USER_DATA_PATH}/poster.png`,
								success(res1){
									console.log(res1)
									let qrcode = res1.path
									wx.getImageInfo({
										src: that.poster,
										success(res2){
											console.log(res2)
											ctx.drawImage(res2.path, 0, 0, 300, 533);
											ctx.drawImage(qrcode, 75, 333, 150, 150);
											ctx.draw();	
										},
										fail(err){
											console.log(err)
										}
									})
								},
								fail(err){
									console.log(err)
								}
							})
						},
						fail(err){
							console.log(err)
						}
					})

				})
			},
			sharePoster() {
				wx.canvasToTempFilePath({
					canvasId: "canvasId",
					success: (res) => {
						console.log("分享海报绘制完成:", res.tempFilePath);
						wx.showShareImageMenu({
							path: res.tempFilePath,
						});
					},
					fail: (err) => {
						console.error("导出图片失败", err);
					},
				});
			},
		}
	}
</script>

<style lang="less" scoped>
	.box {
		width: 300px;
		height: 533px;
	}
	.btn {
		width: 300px;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		background: #FF392C;
		border-radius: 15rpx;
		color: #fff;
	}
	.topVue {
		width: 100%;
		background: #fff;
		position: fixed;
		left: 0;
		top: 0;
		z-index: 99;
		.safeVue {
			width: 100%;
		}
	}
</style>